package com.bl.weilan.service.biz.companytrade.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.ListUtils;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.*;
import com.bl.weilan.dao.repository.*;
import com.bl.weilan.service.biz.companytrade.CompanyTradeService;
import com.bl.weilan.service.biz.tradedetail.TradeDetailService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companystockholder.CompanyStockholderModVo;
import com.bl.weilan.service.vo.companytrade.CompanyTradeAddVo;
import com.bl.weilan.service.vo.companytrade.CompanyTradeListVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class CompanyTradeServiceImpl implements CompanyTradeService {
    @Autowired
    CompanyTradeRepository companyTradeRepository;

    @Autowired
    TradeDetailService tradeDetailService;

    @Autowired
    CompanyRepository companyRepository;

    @Autowired
    CompanyFinancingRepository companyFinancingRepository;

    @Autowired
    TradeInstitutionRepository tradeInstitutionRepository;

    @Autowired
    InstitutionCompanyRepository institutionCompanyRepository;

    @Override
    public CompanyTradeListVo getListByCompanyId(String companyId ){
        List<CompanyTradeEntity> companyTradeEntities = new ArrayList<>();;
        CompanyTradeListVo companyTradeListVo = new CompanyTradeListVo();

        companyTradeEntities = companyTradeRepository.findByCompanyId(companyId);


        companyTradeListVo.setTotalCount(companyTradeEntities.size());
        companyTradeListVo.setList(companyTradeEntities);

        return companyTradeListVo;
    }

//    @Override
//    public CompanyTradeListVo getListByTradeBId(String tradeBId, Pageable pageable ){
//        Page<CompanyTradeEntity> companyTradeEntities;
//        CompanyTradeListVo companyTradeListVo = new CompanyTradeListVo();
//
//        companyTradeEntities = companyTradeRepository.findByTradeB(tradeBId,pageable);
//        companyTradeListVo.setTotalCount(companyTradeEntities.getTotalElements());
//        companyTradeListVo.setList(companyTradeEntities.getContent());
//
//        return companyTradeListVo;
//    }
//
//    @Override
//    public CompanyTradeListVo getListByTradeCId(String tradeCId, Pageable pageable ){
//        Page<CompanyTradeEntity> companyTradeEntities;
//        CompanyTradeListVo companyTradeListVo = new CompanyTradeListVo();
//        companyTradeEntities = companyTradeRepository.findByTradeC(tradeCId,pageable);
//
//        companyTradeListVo.setTotalCount(companyTradeEntities.getTotalElements());
//        companyTradeListVo.setList(companyTradeEntities.getContent());
//
//        return companyTradeListVo;
//    }


    @Override
    public void addCompanyTrade(CompanyTradeAddVo companyTradeAddVo) {

        CompanyEntity companyEntity = companyRepository.findOne(companyTradeAddVo.getCompanyId());

        CompanyTradeEntity companyTradeEntity = new CompanyTradeEntity();
        companyTradeEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companyTradeAddVo, companyTradeEntity);
        companyTradeRepository.save(companyTradeEntity);

        // 投资机构行业关联
        List<InstitutionCompanyEntity> institutionCompanyEntities = institutionCompanyRepository.findByCompanyId(companyTradeAddVo.getCompanyId());
        for (InstitutionCompanyEntity institutionCompanyEntity : institutionCompanyEntities) {
            TradeInstitutionEntity tradeInstitutionEntityA = tradeInstitutionRepository.findByTradeIdAndInstitutionId(companyTradeAddVo.getTradeB(), institutionCompanyEntity.getInstitutionId());
            if (tradeInstitutionEntityA == null) {
                tradeInstitutionEntityA = new TradeInstitutionEntity();
                tradeInstitutionEntityA.setTradeId(companyTradeAddVo.getTradeB());
                tradeInstitutionEntityA.setInstitutionId(institutionCompanyEntity.getInstitutionId());
                tradeInstitutionEntityA.setId(UUIDUtils.getUUID());
                tradeInstitutionEntityA.setNum(1);
                tradeInstitutionEntityA.setUpdateTime(new Date());
                tradeInstitutionRepository.save(tradeInstitutionEntityA);
            } else {
                tradeInstitutionEntityA.setNum(tradeInstitutionEntityA.getNum() + 1);
                tradeInstitutionEntityA.setUpdateTime(new Date());
                tradeInstitutionRepository.save(tradeInstitutionEntityA);
            }

            TradeInstitutionEntity tradeInstitutionEntityB = tradeInstitutionRepository.findByTradeIdAndInstitutionId(companyTradeAddVo.getTradeC(), institutionCompanyEntity.getInstitutionId());
            if (tradeInstitutionEntityB == null) {
                tradeInstitutionEntityB = new TradeInstitutionEntity();
                tradeInstitutionEntityB.setTradeId(companyTradeAddVo.getTradeC());
                tradeInstitutionEntityB.setInstitutionId(institutionCompanyEntity.getInstitutionId());
                tradeInstitutionEntityB.setId(UUIDUtils.getUUID());
                tradeInstitutionEntityB.setNum(1);
                tradeInstitutionEntityB.setUpdateTime(new Date());
                tradeInstitutionRepository.save(tradeInstitutionEntityB);
            } else {
                tradeInstitutionEntityB.setUpdateTime(new Date());
                tradeInstitutionEntityA.setNum(tradeInstitutionEntityB.getNum() + 1);
                tradeInstitutionRepository.save(tradeInstitutionEntityB);
            }
        }

        Date date = companyEntity.getFoundTime();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        // 成立时间
        tradeDetailService.addFoundTime(companyTradeAddVo.getTradeB(), year + "");
        tradeDetailService.addFoundTime(companyTradeAddVo.getTradeC(), year + "");
        // 成立地点
        tradeDetailService.addFoundLocation(companyTradeAddVo.getTradeB(), companyEntity.getCity());
        tradeDetailService.addFoundLocation(companyTradeAddVo.getTradeC(), companyEntity.getCity());

        List<CompanyFinancingEntity> companyFinancingEntities;
        companyFinancingEntities = companyFinancingRepository.findByCompanyIdOrderByTimeDesc(companyEntity.getId());
        for (CompanyFinancingEntity companyFinancingEntity : companyFinancingEntities) {
            Date date1 = companyEntity.getFoundTime();
            Calendar c1 = Calendar.getInstance();
            c1.setTime(date1);
            int year1 = c.get(Calendar.YEAR);
            // 行业增加总估值
            tradeDetailService.addValuations(companyTradeAddVo.getTradeB(), year1, (int)companyFinancingEntity.getValuation());
            tradeDetailService.addValuations(companyTradeAddVo.getTradeC(), year1, (int)companyFinancingEntity.getValuation());
            // 投资金额
            tradeDetailService.addFinacing(companyTradeAddVo.getTradeB(), year1, (int)companyFinancingEntity.getMoney());
            tradeDetailService.addFinacing(companyTradeAddVo.getTradeC(),year1, (int)companyFinancingEntity.getMoney());
            // 投资轮次
            tradeDetailService.addFinacingRound(companyTradeAddVo.getTradeB(), companyFinancingEntity.getRounds(), year1);
            tradeDetailService.addFinacingRound(companyTradeAddVo.getTradeC(), companyFinancingEntity.getRounds(), year1);
            // 投资事件
            tradeDetailService.addFinacingThing(companyTradeAddVo.getTradeB(),year1);
            tradeDetailService.addFinacingThing(companyTradeAddVo.getTradeC(),year1);
        }
    }

    @Override
    public void del(DelBody id) {
        CompanyTradeEntity companyTradeEntity = companyTradeRepository.findOne(id.getId());
        companyTradeRepository.delete(id.getId());
        // 删除投资机构行业关联
        List<InstitutionCompanyEntity> institutionCompanyEntities = institutionCompanyRepository.findByCompanyId(companyTradeEntity.getCompanyId());
        for (InstitutionCompanyEntity institutionCompanyEntity : institutionCompanyEntities) {
            TradeInstitutionEntity tradeInstitutionEntity = tradeInstitutionRepository.findByTradeIdAndInstitutionId(companyTradeEntity.getTradeC(), institutionCompanyEntity.getInstitutionId());
            tradeInstitutionEntity.setNum(tradeInstitutionEntity.getNum() - 1);
            if (tradeInstitutionEntity.getNum() == 0) {
                tradeInstitutionRepository.delete(tradeInstitutionEntity.getId());
            }

            TradeInstitutionEntity tradeInstitutionEntity1 = tradeInstitutionRepository.findByTradeIdAndInstitutionId(companyTradeEntity.getTradeB(), institutionCompanyEntity.getInstitutionId());
            tradeInstitutionEntity1.setNum(tradeInstitutionEntity1.getNum() - 1);
            if (tradeInstitutionEntity1.getNum() == 0) {
                tradeInstitutionRepository.delete(tradeInstitutionEntity1.getId());
            }
        }

    }

//    @Override
//    public void delList(DelListBody idList) {
//        for(String id : idList.getList()) {
//            companyTradeRepository.delete(id);
//        }
//    }

}
